<?php
namespace Admin\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
        $test= M("test");

        $count=$test->count();
        $new_test=$test->field(
            array('id','title','name','time','uptime','select_point','blank_point','judge_point','total_point')
        )
            ->order('id desc')
            ->select();
        $new_test=$this->filter($new_test);
        $this->assign("username",session('username'));
        $this->assign("data",$new_test);
        $this->assign('news_count',$count);
        session("title",$new_test["title"]);
         $this->display();

    }
    private  function  filter($parem){
        foreach ($parem as $key=>$value){

            $parem[$key]['time']=date('Y-m-d H:i:s',$value['time']);
            if(!$value['uptime']){
                $parem[$key]['uptime']='无';
            }else{
                $parem[$key]['uptime']=date('Y-m-d H:i:s',$value['uptime']);
                $parem[$key]['time']=date('Y-m-d H:i:s',$value['time']);
            }
            if(mb_strlen($value['title'],'utf-8')>8){
                $parem[$key]['title']=mb_substr($value['title'],0,8,'utf-8').'...';
            }
        }
        return $parem;
    }


    public function  edit($id){
        if (session('username')){
           // redirect(U('/Admin/Updata/index',0,array('id'=>$id)),'');
            redirect(U('/Admin/Updata/index',array('id'=>$id,'username'=>session('username'))),0,"修改试题");
        }else{
            redirect(U('/Admin/Login/index'),0,'重新登陆');
        }

    }
    public  function  add(){
        redirect(U('/Admin/EditMessage/index'),0,'试题信息添加');
    }
    public  function  up($id){
        $this->uptime($id);
        redirect(U('/Admin/EditMessage/index',array('id'=>$id)),0,'试题信息修改');
    }
    public function  delete($id){
        $article=M('test');
        $a= $article->where('`id`='.$id)->delete();
        if($a){
            $this->success('该试题删除成功',U('/Admin/Index/index',array('id'=>$_SESSION["id"])));
        }else{
            $this->error('该试题删除失败');
        }
    }
    public function uptime($id){
        $text = M('test');
        $data=array(
            "uptime"=>time(),
        );
        $text->where('`id`='.$id)->setField($data);
    }

    public  function  quit(){
            redirect(U('/Admin/Login/index'));


    }




//    public function ks_select(){
//        $text=I("text");
//        $test="下列选项中，不是URL地址中所包含的信息是（）A主机名B端口号C网络协议D软件版本";
//
//        $tm=explode('A',$test);
//        $bc_tm=$tm["0"];
//        var_dump($bc_tm);
//
//        $a=explode('B',$tm["1"]);
//        $bc_a=$a["0"];
//        var_dump($bc_a);
//
//        $b=explode('C',$a["1"]);
//        $bc_b=$b["0"];
//        var_dump($bc_b);
//
//        $c=explode('D',$b["1"]);
//        $bc_c=$c["0"];
//        var_dump($bc_c);
//        $bc_d=$c["1"];
//        var_dump($bc_d);
//    }
}